package com.yanqu.road.dao.impl.activity.jiuzhou.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.CrossJiuZhouDeployData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJiuZhouDeployDataDaoImpl extends TempDataObjectDao<CrossJiuZhouDeployData> {
    
    public static String getCrossJiuZhouDeployDataSql = "select * from `t_c_jiu_zhou_deploy_data` where `activityId`=? ";

    public Map<Integer, Map<Long, List<CrossJiuZhouDeployData>>> getCrossJiuZhouDeployDataMap(int activityId) {
        Map<Integer, Map<Long, List<CrossJiuZhouDeployData>>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossJiuZhouDeployDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossJiuZhouDeployData deployData = parse(rs);
                Map<Long, List<CrossJiuZhouDeployData>> map1 = map.getOrDefault(deployData.getRoomId(), new ConcurrentHashMap<>());
                List<CrossJiuZhouDeployData> list = map1.getOrDefault(deployData.getUserId(), new ArrayList<>());
                list.add(deployData);
                map1.put(deployData.getUserId(), list);
                map.put(deployData.getRoomId(), map1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossJiuZhouDeployDataSql = "update `t_c_jiu_zhou_deploy_data` set skillId2=?,silverBuff=?,skillId1List=?,patronsIdList=? where activityId=? and round=? and roomId=? and userId=? and opponentUserId=? ";
    @Override
    protected boolean updateData(CrossJiuZhouDeployData crossJiuZhouDeployData) {
        crossJiuZhouDeployData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getSkillId2()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouDeployData.getSilverBuff().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouDeployData.getSkillId1List())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouDeployData.getPatronsIdList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getRoomId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouDeployData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouDeployData.getOpponentUserId()));
        if (getDb().executeNoneQuery(updateCrossJiuZhouDeployDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossJiuZhouDeployDataSql = "insert into `t_c_jiu_zhou_deploy_data`(activityId,round,roomId,userId,opponentUserId,skillId2,silverBuff,skillId1List,patronsIdList) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossJiuZhouDeployData crossJiuZhouDeployData) {
        crossJiuZhouDeployData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getRoomId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouDeployData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossJiuZhouDeployData.getOpponentUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossJiuZhouDeployData.getSkillId2()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossJiuZhouDeployData.getSilverBuff().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouDeployData.getSkillId1List())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossJiuZhouDeployData.getPatronsIdList())));
        if (getDb().executeNoneQuery(insertCrossJiuZhouDeployDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossJiuZhouDeployData parseData(ResultSet rs) throws SQLException {
        CrossJiuZhouDeployData data = new CrossJiuZhouDeployData();
        data.setActivityId(rs.getInt("activityId"));
        data.setRound(rs.getInt("round"));
        data.setRoomId(rs.getInt("roomId"));
        data.setUserId(rs.getLong("userId"));
        data.setOpponentUserId(rs.getLong("opponentUserId"));
        data.setSkillId2(rs.getInt("skillId2"));
        String silverBuff = rs.getString("silverBuff");
        if (!StringUtils.isNullOrEmpty(silverBuff)) {
            data.setSilverBuff(new BigInteger(silverBuff));
        }
        String skillId1List = rs.getString("skillId1List");
        if (!StringUtils.isNullOrEmpty(skillId1List)) {
            data.setSkillId1List(JSON.parseObject(skillId1List, new TypeReference<List<Integer>>() {
            }));
        }
        String patronsIdList = rs.getString("patronsIdList");
        if (!StringUtils.isNullOrEmpty(patronsIdList)) {
            data.setPatronsIdList(JSON.parseObject(patronsIdList, new TypeReference<List<Integer>>() {
            }));
        }
        return data;
    }
}
